package liuliu.throughwaring.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import net.tsz.afinal.annotation.view.CodeNote;

import java.util.ArrayList;
import java.util.List;

import liuliu.custom.control.dialog.LLDialog;
import liuliu.custom.control.toolbar.TToolbar;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.adapter.CommonAdapter;
import liuliu.throughwaring.adapter.ViewHolder;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.control.add.police.person.IRegPersonView;
import liuliu.throughwaring.control.add.police.person.RegPersonListener;
import liuliu.throughwaring.model.CodeModel;
import liuliu.throughwaring.model.CompanyModel;
import liuliu.throughwaring.model.PersonModel;
import liuliu.throughwaring.model.TransactionModel;

/**
 * 添加人员事务列表
 * Created by Administrator on 2015/12/8.
 */
public class TransactionListActivity extends BaseActivity implements IRegPersonView {
    //
    @CodeNote(id = R.id.transaction_company_name)
    TextView cname;//单位名称@CodeNote(id = R.id.transaction_perosn_type)
    //    TextView ptype;//人员类型
    @CodeNote(id = R.id.transaction_list_add)
    Button add;//添加人员
    @CodeNote(id = R.id.transaction_list_lv)
    ListView lv;//人员列表
    List<PersonModel> list;
    List<CodeModel> codeListPersonState;
    List<CodeModel> codeListCompanyType;
    String transactionId = "0";//事务编码
    CompanyModel com;
    @CodeNote(id = R.id.transaction_list_toolbar)
    TToolbar toolbar;
    private LLDialog llDialog;
    RegPersonListener mListener;
    boolean isNull = true;

    @Override
    public void initViews() {
        setContentView(R.layout.activity_transaction_list);
        mListener = new RegPersonListener(this, this);
        com = (CompanyModel) getIntent().getSerializableExtra("CompanyModel");
        transactionId = getIntent().getStringExtra("TransactionId");
        codeListPersonState = finalDb.findAllByWhere(CodeModel.class, "codeName='Code_PersonState'");
        codeListCompanyType = finalDb.findAllByWhere(CodeModel.class, "codeName='Code_CompanyType'");
//        ptype.setText("人员类型：" + getCodeValue(codeListCompanyType, com.getCompanyType()));
        cname.setText("单位名称：" + com.getCompanyName());
        list = new ArrayList<PersonModel>();
        llDialog = new LLDialog(this);
    }

    @Override
    public void initEvents() {
        toolbar.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                finish();
            }
        });
        toolbar.setRightOnClick(new TToolbar.RightOnClickListener() {
            @Override
            public void rightclick() {
                Intent intent = new Intent();
                intent.putExtra("TransactionId", transactionId);
                Bundle bundle = new Bundle();
                bundle.putSerializable("CompanyModel", com);
                intent.putExtras(bundle);
                intent.setClass(TransactionListActivity.this, RegPersonActivity.class);
                isNull = false;
                startActivityForResult(intent, 13);
            }
        });
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("TransactionId", transactionId);
                Bundle bundle = new Bundle();
                bundle.putSerializable("CompanyModel", com);
                intent.putExtras(bundle);
                intent.setClass(TransactionListActivity.this, RegPersonActivity.class);
                isNull = false;
                startActivityForResult(intent, 13);
            }
        });
    }

    /**
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 13) {
            if (resultCode == 131) {
                final PersonModel p = (PersonModel) data.getSerializableExtra("PersonModel");
                list.add(p);
                CommonAdapter<PersonModel> adapter = new CommonAdapter<PersonModel>(this, list, R.layout.item_main_person_search) {
                    @Override
                    public void convert(ViewHolder holder, final PersonModel model, int position) {
                        holder.setText(R.id.person_search_item_main_user_name, model.getPersonName());
                        holder.setText(R.id.person_search_item_main_user_state, getCodeValue(codeListPersonState, model.getPersonStatus()));
                        holder.setText(R.id.person_search_item_main_user_unit, model.getPersonComapnyName());
                        holder.setText(R.id.person_search_item_main_create_time, model.getPersonCreateTime());
                        if (!model.getPersonCardImage().equals("")) {
                            holder.setImageBitmap(R.id.person_search_item_main_user_img, Utils.getBitmapByte(model.getPersonCardImage()));
                        }else{
                            holder.setImageResource(R.id.person_search_item_main_user_img,R.mipmap.person_header);
                        }
//                        String mes="离职";
//                        if (model.getPersonStatus().equals("OnJob")) {
//                            holder.setButtonText(R.id.person_search_item_main_leave_job,"离职");
//                            holder.setVisible(R.id.person_search_item_main_leave_job, true);
//                        } else if(model.getPersonStatus().equals("CheckIn")){
//                            holder.setButtonText(R.id.person_search_item_main_leave_job,"退租");
//                            holder.setVisible(R.id.person_search_item_main_leave_job, true);
//                            mes="退租";
//                        } else{
                        holder.setVisible(R.id.person_search_item_main_leave_job, false);
                        // }
                        //final String finalMes = mes;
//                        holder.setOnClickListener(R.id.person_search_item_main_leave_job, new View.OnClickListener() {
//                            @Override
//                            public void onClick(View v) {
//                                llDialog.setMiddleMessage("确认要"+ finalMes +"？");
//                                llDialog.setOnPositiveListener(new View.OnClickListener() {
//                                    @Override
//                                    public void onClick(View v) {//通知后台给置顶人员离职
//                                        model.setCheckInTime(Utils.getDateString(model.getCheckInTime()));
//                                        model.setCheckOutTime(model.getCheckInTime());
//                                        if(finalMes.equals("离职")) {
//                                            model.setPersonStatus("LeaveJob");
//                                        }else{
//                                            model.setPersonStatus("CheckOut");
//                                        }
//                                        for (PersonModel p : list) {
//                                            if (p.getPersonId().equals(model.getPersonId())) {
//                                                p.setPersonStatus("LeaveJob");
//                                            }
//                                        }
//                                        mListener.RegPerson(model);
//                                        llDialog.dismiss();
//                                    }
//                                });
//                                llDialog.setOnNegativeListener(new View.OnClickListener() {
//                                    @Override
//                                    public void onClick(View v) {
//                                        llDialog.cancel();
//                                    }
//                                });
//                                llDialog.show();
                        //                      }
                        //                     });
                    }
                };
//                lv.setAdapter(adapter);
            } else {
                if (list.size() == 0) {//没有添加数据
                    isNull = true;
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private String getCodeValue(List<CodeModel> list, String value) {
        for (int i = 0; i < list.size(); i++) {
            if (value.equals(list.get(i).getName())) {
                return list.get(i).getValue();//流动人员
            }
        }
        return "";
    }

    @Override
    public void onRegPersonResult(boolean isTrue, String mes) {
        Utils.ToastShort(TransactionListActivity.this, mes);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (isNull) {
            if (finalDb != null) {//数据库连接不为空，并且没有添加人员信息。（从本地删除事务信息）
                List count = finalDb.findAllByWhere(PersonModel.class, "PersonTransactionId='" + transactionId + "'");
                if (count != null) {
                    if (count.size() == 0) {
                        finalDb.deleteByWhere(TransactionModel.class, "TransactionId='" + transactionId + "'");
                    }
                }
            }
        }
    }
}
